package cn.orange.demo.stopTimer;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.orange.demo.R;

public class stopTimer extends LinearLayout {
    private TextView tvHour,tvMinute,tvSecond,tvMSecond;
    private Button btnStart,btnPause,btnLap,btnReset,btnResume;
    private ListView lapList;
    private final int MSG_SHOW_TIME=1;
    private ArrayAdapter<String> lapAdater;
    private Timer WatchTimer=new Timer();
    private TimerTask countTimerTask=null;
    private TimerTask showTimeTask=null;
    private  int ten_MSecond_count=0;
    
	public stopTimer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public stopTimer(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public void destory() {
		WatchTimer.cancel();
	}
	
	public stopTimer(Context context) {
		super(context);
		
	}
	public Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_SHOW_TIME:
				tvHour.setText(ten_MSecond_count/100/60/60+"");
				tvMinute.setText(ten_MSecond_count/100/60%60+"");
				tvSecond.setText(ten_MSecond_count/100%60+"");
				tvMSecond.setText(ten_MSecond_count%100+"");
				break;
			default:
				break;
			}
		};
	};
	public void stopWatch(){
		if(countTimerTask!=null){
			countTimerTask.cancel();
			countTimerTask=null;
		}
		if(showTimeTask!=null){
			showTimeTask.cancel();
			showTimeTask=null;
		}
		btnReset.setVisibility(View.VISIBLE);
		btnResume.setVisibility(View.VISIBLE);
		btnLap.setVisibility(View.GONE);
		btnPause.setVisibility(View.GONE);
		btnStart.setVisibility(View.GONE);
	}
	public void startWatch(){
		if(countTimerTask==null){
			countTimerTask=new TimerTask() {
				
				@Override
				public void run() {
					ten_MSecond_count++;
					handler.sendEmptyMessage(MSG_SHOW_TIME);
					
				}
			};
			WatchTimer.schedule(countTimerTask, 10, 10);
		}
		
		btnStart.setVisibility(View.GONE);
		btnReset.setVisibility(View.GONE);
		//
		btnLap.setVisibility(View.VISIBLE);
		btnPause.setVisibility(View.VISIBLE);
		
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		btnStart=(Button) findViewById(R.id.btnSWStart);
		btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startWatch();
			}
		});
		showTimeTask=new TimerTask() {
			
			@Override
			public void run() {
				handler.sendEmptyMessage(MSG_SHOW_TIME);
			}
		};
		WatchTimer.schedule(showTimeTask, 200,200);
		btnLap=(Button) findViewById(R.id.btnSWLap);
		btnLap.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				lapAdater.insert(String.format("%d:%d:%d:%d",ten_MSecond_count/100/60/60,ten_MSecond_count/100/60%60,ten_MSecond_count/100%60,ten_MSecond_count%100), 0);
				//lapAdater.setNotifyOnChange(true);
			}
		});
		btnReset=(Button) findViewById(R.id.btnSWReset);
		btnReset.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				stopWatch();
				tvHour.setText("00");
				tvSecond.setText("00");
				tvMinute.setText("00");
				tvMSecond.setText("00");
				lapAdater.clear();
				btnLap.setVisibility(View.GONE);
				btnReset.setVisibility(View.GONE);
				btnPause.setVisibility(View.GONE);
				btnResume.setVisibility(View.GONE);
				btnStart.setVisibility(View.VISIBLE);
			}
		});
		btnPause=(Button) findViewById(R.id.btnSWPause);
		btnPause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				stopWatch();
			}
		});
		btnResume=(Button) findViewById(R.id.btnSWResume);
		btnResume.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startWatch();
				btnLap.setVisibility(View.VISIBLE);
				btnReset.setVisibility(View.VISIBLE);
				btnPause.setVisibility(View.VISIBLE);
				btnResume.setVisibility(View.GONE);
				btnStart.setVisibility(View.GONE);
			}
		});
		tvHour=(TextView) findViewById(R.id.tvHour);
		tvMinute=(TextView) findViewById(R.id.tvMinuter);
		tvSecond=(TextView) findViewById(R.id.tvSecond);
		tvMSecond=(TextView) findViewById(R.id.tvMSecond);
		
		lapList=(ListView) findViewById(R.id.lvWatchTimerList);
		lapAdater=new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1);
		lapList.setAdapter(lapAdater);
		btnLap.setVisibility(View.GONE);
		btnReset.setVisibility(View.GONE);
		btnPause.setVisibility(View.GONE);
		btnResume.setVisibility(View.GONE);
	}

}
